// JavaScript Document

(function ($) {
    function hideOptions(speed) {
        if (speed.data) {
            speed = speed.data
        }
        if ($(document).data("nowselectoptions")) {
            $($(document).data("nowselectoptions")).slideUp(speed);
            $($(document).data("nowselectoptions")).prev("div").removeClass("tag_select_open");
            $(document).data("nowselectoptions", null);
            $(document).unbind("click", hideOptions);
            $(document).unbind("keyup", hideOptionsOnEscKey);
        }
    }

    function hideOptionsOnEscKey(e) {

        var myEvent = e || window.event;
        var keyCode = myEvent.keyCode;
        if (keyCode == 27) hideOptions(e.data);
    }

    function showOptions(speed) {
        $(document).bind("click", speed, hideOptions);
        $(document).bind("keyup", speed, hideOptionsOnEscKey);
        $($(document).data("nowselectoptions")).slideDown(speed);
        $($(document).data("nowselectoptions")).prev("div").addClass("tag_select_open");
    }

    $.fn.selectCss = function (_speed) {
        $(this).each(function () {

            var speed = _speed || "fast";
            if ($(this).data("cssobj")) {
                $($(this).data("cssobj")).remove();
            }
            $(this).hide();
            var div = "<div id=div_" + this.id + "></div>"
            var divselect = $(div).insertAfter(this).addClass("tag_select,tag_select2");
            $(this).data("cssobj", divselect);
            var ul = "<dl id=dl_" + this.id + "></dl>";
            var divoptions = $(ul).insertAfter(divselect).addClass("tag_options").hide();

            var that = this;
            divselect.click(function (e) {
            	if($(that).data("disabled"))return;
                if ($($(document).data("nowselectoptions")).get(0) != $(this).next("dl").get(0)) {
                    hideOptions(speed);
                }
                if (!$(this).next("dl").is(":visible")) {
                    e.stopPropagation();
                    $(document).data("nowselectoptions", $(this).next("dl"));
                    showOptions(speed);
                }
            });
            divselect.hover(function () {
                $(this).addClass("tag_select_hover");
            },

            function () {
                $(this).removeClass("tag_select_hover");
            });
            $(this).change(function () {
                $(this).nextAll("dl").children("dd:eq(" + $(this)[0].selectedIndex + ")").addClass("open_selected").siblings().removeClass("open_selected");
                $(this).next("div").html($(this).children("option:eq(" + $(this)[0].selectedIndex + ")").text());
            });
            $(this).children("option").each(function (i) {
                var lioption = $("<dd></dd>").html($(this).text()).attr("title", $(this).attr("title")).appendTo(divoptions);
                if ($(this).attr("selected")) {
                    lioption.addClass("open_selected");
                   
                    divselect.html($(this).text());
                }
                lioption.data("option", this);
                lioption.click(function () {
                    lioption.data("option").selected = true;
                    $(lioption.data("option")).trigger("change", true)
                });
                lioption.hover(

                function () {
                    $(this).addClass("open_hover");
                },

                function () {
                    $(this).removeClass("open_hover");
                });
            });
        });
    }
})
(jQuery);